#include <stdio.h>

typedef struct Block
{
    size_t size;
    struct  Block* next;
} Block;

Block* memory_pool = NULL;

void* pool_alloc(size_t size)
{
    Block* prev = NULL;
    Block* curr = memory_pool;

    while (curr && !(curr->size >= size))
    {
        prev = curr;
        curr = curr->next;
    }

    if(!curr)
        return NULL;
    
    if(curr->size > size + sizeof(Block))
    {
        Block* new_block = (Block*) ((char*)curr + sizeof(Block) + size);
        new_block->size = curr->size - size - sizeof(Block);
        new_block->next = curr->next;
        curr->size = size;
        curr->next = new_block;
    }

    if(prev)
        prev->next = curr->next;
    else
        memory_pool = curr->next;

    return (void*) (curr + 1);
}

/**
 * 指针实现内存池的碎片整理
*/
int main(int argc, char const *argv[])
{
    
    return 0;
}